﻿// JScript 文件
var objdiv;//提示信息的现实层对象

    function killmess()
    {
        //objdiv.parentElement.remove(objdiv);
        if(objdiv!=null)
        objdiv.style.display="none";
    }
    //绑定验证,
//参数：要验证元素，正则表达式，错误信息,引发验证的按钮id（id1,id2）多个要以逗号隔开
    function boundsource(sourceid,reg,msg,activebutton)
    {
        var source=document.getElementById(sourceid);
        source.attachEvent("onpropertychange",function checkchange()
                                {
                                    var newreg=new RegExp(reg);
                                    if(!newreg.test(source.value))
                                    {
                                        showmess(source,msg);
                                    }
                                    else
                                        killmess();
                                });
        var sact=activebutton.split(',');
        for(js=0;js<sact.length;js++)
        {
            var sub=document.getElementById(sact[js]);
            //alert(sact[js]);
            if(sub!=null)
            {
            sub.attachEvent("onclick",function su()
                                            {
                                                var newreg=new RegExp(reg);
                                                //alert(source.value+":");
                                                if(!newreg.test(source.value))
                                                {
                                                    if(objdiv==null||objdiv.style.display=="none")
                                                    {   
                                                        showmess(source,msg);
                                                        source.focus();    
                                                    }
                                                    return false; 
                                                }
                                            });
            }
          }
        source.attachEvent("onfocus",function checkblur()
                        {
                            var newreg=new RegExp(reg);
                            if(!newreg.test(source.value))
                            {
                                showmess(source,msg);
                            }
                            else
                                killmess();
                        });
        source.attachEvent("onblur",function checkblur()
                        {
                                killmess();
                        });
    }
//在元素下方显示提示信息，source是要显示信息的元素，msg要显示的信息
    function showmess(source,msg)
    {
        if(objdiv==null)
            objdiv=document.createElement("DIV");
        var hei=source.offsetHeight;
        var loc=getElementPos(source.id);
        var left=loc.x;
        var top=loc.y+hei;
        objdiv.style.position="absolute";
        objdiv.style.backgroundColor="#ECE9D8";
        objdiv.style.color="#FF0000";
        objdiv.style.zIndex="9999";
        objdiv.style.border="solid 1px #FFAA25";
        objdiv.style.fontFamily="Arial";
        objdiv.style.fontSize="11px";
        objdiv.innerText=msg;
        objdiv.style.top=top;
        objdiv.style.left=left;
        source.parentElement.appendChild(objdiv);
        objdiv.style.display="";
    }
//获取元素绝对定位，参数：元素id。返回对象obj.x,obj.y
    function getElementPos(elementId) 
    {
         var ua = navigator.userAgent.toLowerCase();
         var isOpera = (ua.indexOf('opera') != -1);
         var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
         var el = document.getElementById(elementId);
         if(el.parentNode === null || el.style.display == 'none') 
         {
            return false;
         }      
         var parent = null;
         var pos = [];     
         var box;     
         if(el.getBoundingClientRect)    //IE
         {         
              box = el.getBoundingClientRect();
              var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
              var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
              return {x:box.left + scrollLeft, y:box.top + scrollTop};
         }
         else if(document.getBoxObjectFor)    // gecko    
         {
              box = document.getBoxObjectFor(el); 
              var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0; 
              var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0; 
              pos = [box.x - borderLeft, box.y - borderTop];
         } 
         else    // safari & opera    
         {
              pos = [el.offsetLeft, el.offsetTop];  
              parent = el.offsetParent;     
              if (parent != el) 
              { 
                   while (parent) 
                   {  
                    pos[0] += parent.offsetLeft; 
                    pos[1] += parent.offsetTop; 
                    parent = parent.offsetParent;
                   }  
              }   
              if (ua.indexOf('opera') != -1 || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' ))
              { 
                   pos[0] -= document.body.offsetLeft;
                   pos[1] -= document.body.offsetTop;            
              }    
         }              
         if (el.parentNode) 
         { 
            parent = el.parentNode;
         } 
         else
         {
            parent = null;
         }
         while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') { // account for any scrolled ancestors
          pos[0] -= parent.scrollLeft;
          pos[1] -= parent.scrollTop;
          if (parent.parentNode) {
           parent = parent.parentNode;
          } else {
           parent = null;
          }
         }
         return {x:pos[0], y:pos[1]};
    }
    
    ///    
function showclose(objshow) {
var obj=document.getElementById(objshow);
if(obj.style.display=="none")
   obj.style.display="block";
else
   obj.style.display="none";
}